package web;

import Persistencia.Fisioterapeuta;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import controladores.ControladorAutenticar;
import controladores.ControladorFisioterapeuta;
import dominio.busquedaFisioterapeuta;
import dominio.FisioterapeutaDomain;
import dominio.login;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 
 * 
 */
public class ControladorSimpleFisioterapeutas extends SimpleFormController{

    ControladorFisioterapeuta controlador = new ControladorFisioterapeuta();
    
    @Override
    public ModelAndView onSubmit(Object command) throws ServletException {
        System.out.println("getFormView  :=>  " +this.getCommandName());
        String tipo = ((busquedaFisioterapeuta) command).getTipoBusqueda();
        String id = ((busquedaFisioterapeuta) command).getIdFisio();
        logger.info("tipo es "+tipo);
        logger.info("id es "+id);
        if(tipo.equals("modificar")){
            Fisioterapeuta ob = null;
            List<Fisioterapeuta> obj = controlador.encontrarFisioterapeutas();
            System.out.println(obj);
            if(obj!=null){
                for(int i=0;i<obj.size();i++){
                    if(id.equals(obj.get(i).getIdFisioterapeuta())){
                        ob = obj.get(i);
                    }
                }
            }
            if(ob!=null){
                System.out.println("ob  ---> "+ob);
                return new ModelAndView("modificarFisioterapeuta","addFisio",ob);
            }else{
                return new ModelAndView(new RedirectView(getSuccessView()));
            }
        }else{
            List<Fisioterapeuta> obj = controlador.encontrarFisioterapeutas();
            return new ModelAndView(new RedirectView(getSuccessView()));
        }
    }

    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
        busquedaFisioterapeuta log = new busquedaFisioterapeuta();
        return log;
    }

    public void setControladorFisioterapeuta(ControladorFisioterapeuta productManager) {
        this.controlador = productManager;
    }

    public ControladorFisioterapeuta getControladorFisioterapeuta() {
        return controlador;
    }
}
